<?php
declare(strict_types=1);

namespace HttpServer;


use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
use Snowflake\Abstracts\TraitApplication;
use Snowflake\Snowflake;

/**
 * Class WebController
 * @package Snowflake\Snowflake\Web
 * @property-read HttpParams $input
 * @property-read HttpHeaders $headers
 * @property-read Request $request
 */
class Controller extends HttpService
{

	use TraitApplication;


	/**
	 * Controller constructor.
	 * @param array $config
	 * @throws Exception
	 */
	public function __construct($config = [])
	{
		parent::__construct($config);
	}


	/**
	 * @throws Exception
	 */
	public function init()
	{
		$annotation = Snowflake::getAnnotation();
		$annotation->injectProperty($this);
	}


	/**
	 * @return ?HttpParams
	 * @throws Exception
	 */
	public function getInput(): ?HttpParams
	{
		return \request()->params;
	}

	/**
	 * @return ?HttpHeaders
	 * @throws Exception
	 */
	public function getHeaders(): ?HttpHeaders
	{
		return \request()->headers;
	}

	/**
	 * @return Request|null
	 * @throws Exception
	 */
	public function getRequest(): ?Request
	{
		return \request();
	}


	/**
	 * @param $name
	 * @return mixed
	 * @throws Exception
	 */
	public function __get($name): mixed
	{
		// TODO: Change the autogenerated stub
		if (property_exists($this, $name)) {
			return $this->$name;
		}

		$method = 'get' . ucfirst($name);
		if (method_exists($this, $method)) {
			return $this->{$method}();
		}

		return Snowflake::app()->get($name);
	}


}
